package com.zxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.mapper.SysOperLogMapper;
import com.zxy.pojo.dto.OperLogQueryDTO;
import com.zxy.pojo.entity.SysOperLog;
import com.zxy.pojo.vo.CourseContractResponseVO;
import com.zxy.pojo.vo.OperLogVO;
import com.zxy.service.SysOperLogService;
import com.zxy.util.Constant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统操作日志 Service 实现类
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService {
    @Autowired
    private SysOperLogMapper sysOperLogMapper;

    //分页查询日志信息
    @Override
    public CourseContractResponseVO<OperLogVO> selectOperLog(OperLogQueryDTO dto) {
        Page<SysOperLog> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<SysOperLog>  queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(dto.getKeyWord())) {
            String keyWord = "%" + dto.getKeyWord() + "%";
            queryWrapper.like(SysOperLog::getOperName, keyWord)
                    .or()
                    .like(SysOperLog::getTitle, keyWord)
                    .or()
                    .like(SysOperLog::getOperUrl, keyWord);
        }
        if (dto.getBeginTime() != null) {
            queryWrapper.ge(SysOperLog::getOperTime,dto.getBeginTime());
        }
        if (dto.getEndTime() != null) {
            queryWrapper.le(SysOperLog::getOperTime, dto.getEndTime());
        }
        queryWrapper.orderByDesc(SysOperLog::getOperTime);
        Page<SysOperLog> pages = this.page(page, queryWrapper);
        List<SysOperLog> records = pages.getRecords();
        List<OperLogVO> vos = new ArrayList<>();
        for (SysOperLog operLog : records) {
            OperLogVO operLogVO = new OperLogVO();
            BeanUtils.copyProperties(operLog, operLogVO);
            vos.add(operLogVO);
        }
        CourseContractResponseVO<OperLogVO> responseVO = new CourseContractResponseVO<>();
        responseVO.setTotal(pages.getTotal());
        responseVO.setRows(vos);
        responseVO.setCode(Constant.SUCCESS);
        responseVO.setMsg(Constant.MSG);
        responseVO.setParams(null);
        return responseVO;
    }
}